#!/bin/bash
source /etc/openvpn/peizhi.cfg
status=0
while true
do
	cat $1 | while read line
	do
		flag=`echo $line | cut -d \, -f 1`
		#echo "$line"
		if [[ "$flag" == "Common Name" ]];then
			status=1
			continue 
		fi 
		if [ $status == 1 ];then
			if [[ "$flag" == "ROUTING TABLE" ]];then
				status=0
				break
			else
				username="$flag"
				recv=`echo $line | cut -d \, -f 3`
				sent=`echo $line | cut -d \, -f 4`
				read log < <(mysql -h$localhost -u$root -p$mima -N -e "use ov;select irecv,isent,maxll from openvpn where iuser='$username' limit 1;")
				data_recv=`echo $log | cut -d \  -f 1`
				data_sent=`echo $log | cut -d \  -f 2`
				maxll=`echo $log | cut -d \  -f 3`
				
				count=$[$data_recv+$data_sent+$recv+$sent+0]

				if [ -n "$maxll" ];then
					if [ "$maxll" -lt "$count" ];then
						/bin/Monsha "$username" $2 >/dev/null 2>&1
						echo  $(date +%Y年%m月%d日%k时%M分)成功清理超出流量用户:"$username ""</br>"
					fi
				fi
				sleep 1
			fi
		fi
	done
	sleep 5
done
